package com.wanmait.shoppingsys.controller.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.shoppingsys.dao.SpecialtyDAO;
import com.wanmait.shoppingsys.util.Favorite;
import com.wanmait.shoppingsys.util.ShopCar;
import com.wanmait.shoppingsys.vo.Specialty;
import com.wanmait.shoppingsys.vo.UserInfo;

@WebServlet("/ShopCarController")
public class ShopCarController extends HttpServlet {
	private SpecialtyDAO specialtyDAO = SpecialtyDAO.getDAO();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		if(action == null)
		{
			return;
		}
		switch(action)
		{
		case "buy":				//添加购物车
			this.buy(request, response);
			break;
		case "addCount":	//＋
			this.addCount(request, response);
			break;
		case "diffCount":	//-
			this.diffCount(request, response);
			break;
		case "clear":		//清空购物车
			this.clear(request, response);
			break;
		case "delete":		//删除某个
			this.delete(request, response);
			break;
		case "addbuy":				//添加收藏
			this.addbuy(request, response);
			break;
		case "deletesomeone":		//删除某个
			this.deletesomeone(request, response);
			break;
		}
		
	}
	protected void deletesomeone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletContext().getContextPath();
		String idStr = request.getParameter("id");         //删除商品
		Integer id = Integer.parseInt(idStr);
		Favorite fav = (Favorite)request.getSession().getAttribute("fav");
		if(fav==null) {
			fav = new Favorite();
			request.getSession().setAttribute("fav", fav);
		}
		fav.remove(id);
		response.sendRedirect(path+"/userinfo/shoucang.jsp");
		
	}
	
	protected void addbuy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletContext().getContextPath();     //添加收藏夹
		UserInfo userinfo = (UserInfo)request.getSession().getAttribute("userinfo");
		if(userinfo==null) {
			response.sendRedirect(path+"/userinfo/login.jsp");
			return;
		}
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		Specialty specialty = specialtyDAO.findById(id);
		Favorite fav = (Favorite)request.getSession().getAttribute("fav");
		if(fav==null) {
			fav = new Favorite();
			request.getSession().setAttribute("fav", fav);
		}
		fav.addCommoditys(specialty);
		response.sendRedirect(path+"/userinfo/shoucang.jsp");
	}
	
	protected void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletContext().getContextPath();
		UserInfo userinfo = (UserInfo)request.getSession().getAttribute("userinfo");
		if(userinfo==null) {
			response.sendRedirect(path+"/userinfo/login.jsp");
			return;
		}
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		String countStr = request.getParameter("count");
		Integer count = Integer.parseInt(countStr);
		Specialty specialty = specialtyDAO.findById(id);
		specialty.setCount(count);
		ShopCar shopCar = (ShopCar)request.getSession().getAttribute("shopCar");
		if(shopCar==null) {
			shopCar = new ShopCar();
			request.getSession().setAttribute("shopCar", shopCar);
		}
		shopCar.addCommodity(specialty);
		specialtyDAO.changeLike(id);
		response.sendRedirect(path+"/order/index.jsp");
		
	}
	
	protected void addCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		this.changeCount(request, response, 1);
		
	}
	protected void diffCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.changeCount(request, response, -1);	
	}
	
	protected void changeCount(HttpServletRequest request, HttpServletResponse response,int ccount) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		ShopCar shopCar = (ShopCar)request.getSession().getAttribute("shopCar");
		if(shopCar==null) {
			shopCar = new ShopCar();
			request.getSession().setAttribute("shopCar", shopCar);
		}
		int count = shopCar.addCount(id, ccount);
		response.getWriter().print(count+","+shopCar.getTotalPrice()+","+shopCar.getTotalNumber());
	}
	
	protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ShopCar shopCar = (ShopCar)request.getSession().getAttribute("shopCar");
		if(shopCar!=null) {
			shopCar.clear();
			
		}
		
	}
	
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");         //删除商品
		Integer id = Integer.parseInt(idStr);

		ShopCar shopCar = (ShopCar)request.getSession().getAttribute("shopCar");
		if(shopCar==null) {
			shopCar = new ShopCar();
			request.getSession().setAttribute("shopCar", shopCar);
		}
		shopCar.remove(id);
		String path = request.getServletContext().getContextPath();
		response.sendRedirect(path+"/order/index.jsp");
		
	}
	
	
	
	
	
	
	
	
	
	

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
